package adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.bigchun.woodhunter.R;

import java.util.List;

import bean.CodeBean;
import myapp.MyApp;

/**
 * Created by fei on 2017/12/30.
 */

public class CodeRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    public static final int TYPE_BANNER = 0;//头部轮播图
    public static final int TYPE_CLASSIFICATION = 1;//分类
    public static final int TYPE_SPECIAL = 2;//通知和专题页
    public static final int TYPE_ADVERT = 3;//广告
    public static final int TYPE_PREFERRED = 4;//为你优选
    public static final int TYPE_NORMAL = 5;
    public static final int TYPE_BOTTOM = 6;//底部

    private List<CodeBean.DataBean> mDatas;
    private Context context;
    private MyApp myApp;

    public CodeRecyclerViewAdapter(List<CodeBean.DataBean> mDatas, Context context, MyApp myApp) {
        this.mDatas = mDatas;
        this.context = context;
        this.myApp = myApp;
    }

    private View bottomView;

    private OnItemClickListener mListener;

    public void setOnItemClickListener(OnItemClickListener li) {
        mListener = li;
    }






    public void setBottomView(View bottomView) {
        this.bottomView = bottomView;
    }

    public void setDatas(List<CodeBean.DataBean> datas) {
        this.mDatas = datas;
//        notifyDataSetChanged();
//        notifyItemRangeChanged(datas.size()-10,10);
        if (datas.size() > 20){
            notifyItemRangeChanged(datas.size()-20,20);
        }else{
            notifyDataSetChanged();
//            notifyItemRangeChanged(0,datas.size()-1);
        }
    }

    @Override
    public int getItemViewType(int position) {

        if (position == mDatas.size()) return TYPE_BOTTOM;
        return TYPE_NORMAL;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {


        if (bottomView != null && viewType == TYPE_BOTTOM) {
            return new Holder(bottomView);

        }
        View layout = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_code, parent, false);
        return new Holder(layout);
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder viewHolder, final int position) {

        if (getItemViewType(position) == TYPE_BOTTOM) {
            return;
        }
        final int pos = getRealPosition(viewHolder);
        final CodeBean.DataBean bean = mDatas.get(pos);
        if (viewHolder instanceof Holder) {

            ((Holder) viewHolder).tv_code.setText("+"+bean.getCode());
            ((Holder) viewHolder).tv_name.setText(bean.getName_cn());

            if (mListener != null) {
                viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mListener.onItemClick(pos);
                    }
                });
            }

        }
    }





    public int getRealPosition(RecyclerView.ViewHolder holder) {
        int position = holder.getLayoutPosition();
        return position;
    }

    @Override
    public int getItemCount() {
        return mDatas.size() + 1;
    }

    class Holder extends RecyclerView.ViewHolder {

        private TextView tv_name;//超出配送范围
        private TextView tv_code;//超出配送范围

        public Holder(View itemView) {
            super(itemView);
            tv_name = (TextView) itemView.findViewById(R.id.tv_name);
            tv_code = (TextView) itemView.findViewById(R.id.tv_code);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(int position);
    }



}